package user03.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import user03.mapper.TRoleMapper;
import user03.model.TRole;

import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TRoleService extends ServiceImpl<TRoleMapper, TRole> {

    @Autowired
    private TUserService tUserService;

    @Autowired
    private TRoleMenuService tRoleMenuService;

    @Autowired
    private TUserRoleService tUserRoleService;

    public Boolean updateRoleMenus(Long roleId, List<Long> menuIds) {
        Boolean updateRoleMenus = tRoleMenuService.updateRoleMenus(roleId, menuIds);
        if (updateRoleMenus) {
            List<Long> userIds = tUserRoleService.selectUserIdsByRoleId(roleId);
            if (userIds != null && userIds.size() > 0) {
                for (Long userId : userIds) {
                    tUserService.kickUserOff(userId);
                }
            }
        }
        return updateRoleMenus;
    }

}
